capture log close
log using log_2spe_model, replace text

****************************************************************************************************************
** 	TITLE:		commands_2spe_model                                                                           ** 	
**	DATA:       data_2spe_model.dta                                                                           **
**	AUTHORS:    Bruno Jérôme, Véronique Jérôme, Philippe Mongrain, and Richard Nadeau                         **
**  PAPER:      State-Level Forecasts for the 2020 U.S. Presidential Election: Tough Victory Ahead for Biden  **
**	DATE:		August 2020 					                                                              **	
**	VERSION:	Stata 16					                                                                  **	
****************************************************************************************************************

* Open the dataset

use "data_2spe_model.dta", clear

* Version control

version 16.0


**************************************************
** MODEL ESTIMATION AND WITHIN-SAMPLE FORECASTS **
**************************************************

* 2SPE Model, 1980-2016 (see Table 1 in the article)

reg twoincv unemp legcont jpa2 jpa0 ppi5216 ppi8016 indv chavp rhsc dhsc dcds dcrs if election!=2020

estimates store reg2020

* Predicted two-party vote share by state for incumbent candidate (within-sample forecasts), 1980-2016

reg twoincv unemp legcont jpa2 jpa0 ppi5216 ppi8016 indv chavp rhsc dhsc dcds dcrs if election!=2020

predict ftwoincv_w if election!=2020

* Predicted two-party vote share by state for challenger candidate (within-sample forecasts), 1980-2016

gen ftwochav_w = 100 - ftwoincv_w if election!=2020

* Within-sample errors, 1980-2016

generate err_w = ftwoincv_w - twoincv if election!=2020

* Absolute within-sample errors, 1980-2016

generate abserr_w = abs(err_w) if election!=2020

* Within-sample mean absolute error, 1980-2016

egen mabserr_w = mean(abserr_w) if election!=2020

* Predicted Electoral College votes by state for incumbent canadidate, 1980-2016

gen fince_w = elecvote if ftwoincv_w > ftwochav_w & election!=2020
replace fince_w = 0 if fince_w == . & election!=2020

* Predicted Electoral College votes by state for challenger candidate, 1980-2016

gen fchae_w = elecvote if ftwoincv_w < ftwochav_w & election!=2020
replace fchae_w = 0 if fchae_w == . & election!=2020

* Total predicted Electoral College votes by state for incumbent candidate, 1980-2016

egen sum_carter_e_1980_w = sum(fince_w) if election == 1980
egen sum_reagan_e_1984_w = sum(fince_w) if election == 1984
egen sum_bush41_e_1988_w = sum(fince_w) if election == 1988
egen sum_bush41_e_1992_w = sum(fince_w) if election == 1992
egen sum_bclinton_e_1996_w = sum(fince_w) if election == 1996
egen sum_gore_e_2000_w = sum(fince_w) if election == 2000
egen sum_bush43_e_2004_w = sum(fince_w) if election == 2004
egen sum_mccain_e_2008_w = sum(fince_w) if election == 2008
egen sum_obama_e_2012_w = sum(fince_w) if election == 2012
egen sum_hclinton_e_2016_w = sum(fince_w) if election == 2016

gen sum_fince_w = max(sum_carter_e_1980_w, sum_reagan_e_1984_w, sum_bush41_e_1988_w, sum_bush41_e_1992_w, sum_bclinton_e_1996_w, sum_gore_e_2000_w, sum_bush43_e_2004_w, sum_mccain_e_2008_w, sum_obama_e_2012_w, sum_hclinton_e_2016_w)

* Total predicted Electoral College votes by state for challenger candidate, 1980-2016

egen sum_reagan_e_1980_w = sum(fchae_w) if election == 1980
egen sum_mondale_e_1984_w = sum(fchae_w) if election == 1984
egen sum_dukakis_e_1988_w = sum(fchae_w) if election == 1988
egen sum_bclinton_e_1992_w = sum(fchae_w) if election == 1992
egen sum_dole_e_1996_w = sum(fchae_w) if election == 1996
egen sum_bush43_e_2000_w = sum(fchae_w) if election == 2000
egen sum_kerry_e_2004_w = sum(fchae_w) if election == 2004
egen sum_obama_e_2008_w = sum(fchae_w) if election == 2008
egen sum_romney_e_2012_w = sum(fchae_w) if election == 2012
egen sum_trump_e_2016_w = sum(fchae_w) if election == 2016

gen sum_fchae_w = max(sum_reagan_e_1980_w, sum_mondale_e_1984_w, sum_dukakis_e_1988_w, sum_bclinton_e_1992_w, sum_dole_e_1996_w, sum_bush43_e_2000_w, sum_kerry_e_2004_w, sum_obama_e_2008_w, sum_romney_e_2012_w, sum_trump_e_2016_w)

* Predicted winner in each state, 1980-2016

gen fstatewinner_w = "Jimmy Carter" if ftwoincv_w > ftwochav_w & election == 1980
replace fstatewinner_w = "Ronald Reagan" if ftwoincv_w < ftwochav_w & election == 1980
replace fstatewinner_w = "Ronald Reagan" if ftwoincv_w > ftwochav_w & election == 1984
replace fstatewinner_w = "Walter Mondale" if ftwoincv_w < ftwochav_w & election == 1984
replace fstatewinner_w = "George H. W. Bush" if ftwoincv_w > ftwochav_w & election == 1988
replace fstatewinner_w = "Michael Dukakis" if ftwoincv_w < ftwochav_w & election == 1988
replace fstatewinner_w = "George H. W. Bush" if ftwoincv_w > ftwochav_w & election == 1992
replace fstatewinner_w = "Bill Clinton" if ftwoincv_w < ftwochav_w & election == 1992
replace fstatewinner_w = "Bill Clinton" if ftwoincv_w > ftwochav_w & election == 1996
replace fstatewinner_w = "Bob Dole" if ftwoincv_w < ftwochav_w & election == 1996
replace fstatewinner_w = "Al Gore" if ftwoincv_w > ftwochav_w & election == 2000
replace fstatewinner_w = "George W. Bush" if ftwoincv_w < ftwochav_w & election == 2000
replace fstatewinner_w = "George W. Bush" if ftwoincv_w > ftwochav_w & election == 2004
replace fstatewinner_w = "John Kerry" if ftwoincv_w < ftwochav_w & election == 2004
replace fstatewinner_w = "John McCain" if ftwoincv_w > ftwochav_w & election == 2008
replace fstatewinner_w = "Barack Obama" if ftwoincv_w < ftwochav_w & election == 2008
replace fstatewinner_w = "Barack Obama" if ftwoincv_w > ftwochav_w & election == 2012
replace fstatewinner_w = "Mitt Romney" if ftwoincv_w < ftwochav_w & election == 2012
replace fstatewinner_w = "Hillary Clinton" if ftwoincv_w > ftwochav_w & election == 2016
replace fstatewinner_w = "Donald Trump" if ftwoincv_w < ftwochav_w & election == 2016

* Predicted national winner, 1980-2016

gen fnationalwinner_w = "Jimmy Carter" if sum_fince_w >= 270 & election == 1980
replace fnationalwinner_w = "Ronald Reagan" if sum_fchae_w >= 270 & election == 1980
replace fnationalwinner_w = "Ronald Reagan" if sum_fince_w >= 270 & election == 1984
replace fnationalwinner_w = "Walter Mondale" if sum_fchae_w >= 270 & election == 1984
replace fnationalwinner_w = "George H. W. Bush" if sum_fince_w >= 270 & election == 1988
replace fnationalwinner_w = "Michael Dukakis" if sum_fchae_w >= 270 & election == 1988
replace fnationalwinner_w = "George H. W. Bush" if sum_fince_w >= 270 & election == 1992
replace fnationalwinner_w = "Bill Clinton" if sum_fchae_w >= 270 & election == 1992
replace fnationalwinner_w = "Bill Clinton" if sum_fince_w >= 270 & election == 1996
replace fnationalwinner_w = "Bob Dole" if sum_fchae_w >= 270 & election == 1996
replace fnationalwinner_w = "Al Gore" if sum_fince_w >= 270 & election == 2000
replace fnationalwinner_w = "George W. Bush" if sum_fchae_w >= 270 & election == 2000
replace fnationalwinner_w = "George W. Bush" if sum_fince_w >= 270 & election == 2004
replace fnationalwinner_w = "John Kerry" if sum_fchae_w >= 270 & election == 2004
replace fnationalwinner_w = "John McCain" if sum_fince_w >= 270 & election == 2008
replace fnationalwinner_w = "Barack Obama" if sum_fchae_w >= 270 & election == 2008
replace fnationalwinner_w = "Barack Obama" if sum_fince_w >= 270 & election == 2012
replace fnationalwinner_w = "Mitt Romney" if sum_fchae_w >= 270 & election == 2012
replace fnationalwinner_w = "Hillary Clinton" if sum_fince_w >= 270 & election == 2016
replace fnationalwinner_w = "Donald Trump" if sum_fchae_w >= 270 & election == 2016

* Correct state within-sample forecast?

gen statecorrect_w = "Yes" if fstatewinner_w == statewinner & election!=2020
replace statecorrect_w = "No" if fstatewinner_w!=statewinner & election!=2020

* Correct national within-sample forecast?

gen nationalcorrect_w = "Yes" if fnationalwinner_w == nationalwinner & election!=2020
replace nationalcorrect_w = "No" if fnationalwinner_w!=nationalwinner & election!=2020

* Summary

list election abbr twoincv ftwoincv_w ftwochav_w err_w abserr_w mabserr_w ince fince_w chae fchae_w statecorrect_w if election!=2020


*************************************************************
** MODEL ESTIMATION AND JACKKNIFE OUT-OF-SAMPLE FORECASTS  **
*************************************************************

* 2SPE Model, 1980-2016

set more off
tempname jackknife 
postfile `jackknife' election e_intercept e_unemp e_legcont e_jpa2 e_jpa0 e_ppi5216 e_ppi8016 e_indv e_chavp e_rhsc e_dhsc e_dcds e_dcrs using jackknife.dta, replace
foreach i in 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 {
reg twoincv unemp legcont jpa2 jpa0 ppi5216 ppi8016 indv chavp rhsc dhsc dcds dcrs if election!=`i' & election!=2020 
estimates store reg`i'_o
post `jackknife' (`i') (`=_b[_cons]') (`=_b[unemp]') (`=_b[legcont]') (`=_b[jpa2]') (`=_b[jpa0]') (`=_b[ppi5216]') (`=_b[ppi8016]') (`=_b[indv]') (`=_b[chavp]') (`=_b[rhsc]') (`=_b[dhsc]') (`=_b[dcds]') (`=_b[dcrs]')
}
postclose `jackknife' 
use jackknife, clear
list 

* Predicted two-party vote share by state for incumbent candidate (jackknife out-of-sample forecasts), 1980-2016 (see Tables B1 to K1 in the appendix)

use "data_2spe_model.dta", clear
merge m:1 election using "jackknife.dta"
gen ftwoincv_o = e_intercept + (e_unemp*unemp) + (e_legcont*legcont) + (e_jpa2*jpa2) + (e_jpa0*jpa0) + (e_ppi5216*ppi5216) + (e_ppi8016*ppi8016) + (e_indv*indv) + (e_chavp*chavp) + (e_rhsc*rhsc) + (e_dhsc*dhsc) + (e_dcds*dcds) + (e_dcrs*dcrs) if election!=2020

* Predicted two-party vote share by state for challenger candidate (jackknife out-of-sample forecasts), 1980-2016 (see Tables B1 to K1 in the appendix)

gen ftwochav_o = 100 - ftwoincv_o if election!=2020

* Jackknife out-of-sample errors, 1980-2016

generate err_o = ftwoincv_o - twoincv if election!=2020

* Absolute jackknife out-of-sample errors, 1980-2016

generate abserr_o = abs(err_o) if election!=2020

* Jackknife out-of-sample mean absolute error, 1980-2016

egen mabserr_o = mean(abserr_o) if election!=2020

* Predicted Electoral College votes by state for incumbent canadidate, 1980-2016 (see Tables B1 to K1 in the appendix)

gen fince_o = elecvote if ftwoincv_o > ftwochav_o & election!=2020
replace fince_o = 0 if fince_o == . & election!=2020

* Predicted Electoral College votes by state for challenger candidate, 1980-2016 (see Tables B1 to K1 in the appendix)

gen fchae_o = elecvote if ftwoincv_o < ftwochav_o & election!=2020
replace fchae_o = 0 if fchae_o == . & election!=2020

* Total predicted Electoral College votes by state for incumbent candidate, 1980-2016

egen sum_carter_e_1980_o = sum(fince_o) if election == 1980
egen sum_reagan_e_1984_o = sum(fince_o) if election == 1984
egen sum_bush41_e_1988_o = sum(fince_o) if election == 1988
egen sum_bush41_e_1992_o = sum(fince_o) if election == 1992
egen sum_bclinton_e_1996_o = sum(fince_o) if election == 1996
egen sum_gore_e_2000_o = sum(fince_o) if election == 2000
egen sum_bush43_e_2004_o = sum(fince_o) if election == 2004
egen sum_mccain_e_2008_o = sum(fince_o) if election == 2008
egen sum_obama_e_2012_o = sum(fince_o) if election == 2012
egen sum_hclinton_e_2016_o = sum(fince_o) if election == 2016

gen sum_fince_o = max(sum_carter_e_1980_o, sum_reagan_e_1984_o, sum_bush41_e_1988_o, sum_bush41_e_1992_o, sum_bclinton_e_1996_o, sum_gore_e_2000_o, sum_bush43_e_2004_o, sum_mccain_e_2008_o, sum_obama_e_2012_o, sum_hclinton_e_2016_o)

* Total predicted Electoral College votes by state for challenger candidate, 1980-2016

egen sum_reagan_e_1980_o = sum(fchae_o) if election == 1980
egen sum_mondale_e_1984_o = sum(fchae_o) if election == 1984
egen sum_dukakis_e_1988_o = sum(fchae_o) if election == 1988
egen sum_bclinton_e_1992_o = sum(fchae_o) if election == 1992
egen sum_dole_e_1996_o = sum(fchae_o) if election == 1996
egen sum_bush43_e_2000_o = sum(fchae_o) if election == 2000
egen sum_kerry_e_2004_o = sum(fchae_o) if election == 2004
egen sum_obama_e_2008_o = sum(fchae_o) if election == 2008
egen sum_romney_e_2012_o = sum(fchae_o) if election == 2012
egen sum_trump_e_2016_o = sum(fchae_o) if election == 2016

gen sum_fchae_o = max(sum_reagan_e_1980_o, sum_mondale_e_1984_o, sum_dukakis_e_1988_o, sum_bclinton_e_1992_o, sum_dole_e_1996_o, sum_bush43_e_2000_o, sum_kerry_e_2004_o, sum_obama_e_2008_o, sum_romney_e_2012_o, sum_trump_e_2016_o)

* Predicted winner in each state, 1980-2016

gen fstatewinner_o = "Jimmy Carter" if ftwoincv_o > ftwochav_o & election == 1980
replace fstatewinner_o = "Ronald Reagan" if ftwoincv_o < ftwochav_o & election == 1980
replace fstatewinner_o = "Ronald Reagan" if ftwoincv_o > ftwochav_o & election == 1984
replace fstatewinner_o = "Walter Mondale" if ftwoincv_o < ftwochav_o & election == 1984
replace fstatewinner_o = "George H. W. Bush" if ftwoincv_o > ftwochav_o & election == 1988
replace fstatewinner_o = "Michael Dukakis" if ftwoincv_o < ftwochav_o & election == 1988
replace fstatewinner_o = "George H. W. Bush" if ftwoincv_o > ftwochav_o & election == 1992
replace fstatewinner_o = "Bill Clinton" if ftwoincv_o < ftwochav_o & election == 1992
replace fstatewinner_o = "Bill Clinton" if ftwoincv_o > ftwochav_o & election == 1996
replace fstatewinner_o = "Bob Dole" if ftwoincv_o < ftwochav_o & election == 1996
replace fstatewinner_o = "Al Gore" if ftwoincv_o > ftwochav_o & election == 2000
replace fstatewinner_o = "George W. Bush" if ftwoincv_o < ftwochav_o & election == 2000
replace fstatewinner_o = "George W. Bush" if ftwoincv_o > ftwochav_o & election == 2004
replace fstatewinner_o = "John Kerry" if ftwoincv_o < ftwochav_o & election == 2004
replace fstatewinner_o = "John McCain" if ftwoincv_o > ftwochav_o & election == 2008
replace fstatewinner_o = "Barack Obama" if ftwoincv_o < ftwochav_o & election == 2008
replace fstatewinner_o = "Barack Obama" if ftwoincv_o > ftwochav_o & election == 2012
replace fstatewinner_o = "Mitt Romney" if ftwoincv_o < ftwochav_o & election == 2012
replace fstatewinner_o = "Hillary Clinton" if ftwoincv_o > ftwochav_o & election == 2016
replace fstatewinner_o = "Donald Trump" if ftwoincv_o < ftwochav_o & election == 2016

* Predicted national winner, 1980-2016

gen fnationalwinner_o = "Jimmy Carter" if sum_fince_o >= 270 & election == 1980
replace fnationalwinner_o = "Ronald Reagan" if sum_fchae_o >= 270 & election == 1980
replace fnationalwinner_o = "Ronald Reagan" if sum_fince_o >= 270 & election == 1984
replace fnationalwinner_o = "Walter Mondale" if sum_fchae_o >= 270 & election == 1984
replace fnationalwinner_o = "George H. W. Bush" if sum_fince_o >= 270 & election == 1988
replace fnationalwinner_o = "Michael Dukakis" if sum_fchae_o >= 270 & election == 1988
replace fnationalwinner_o = "George H. W. Bush" if sum_fince_o >= 270 & election == 1992
replace fnationalwinner_o = "Bill Clinton" if sum_fchae_o >= 270 & election == 1992
replace fnationalwinner_o = "Bill Clinton" if sum_fince_o >= 270 & election == 1996
replace fnationalwinner_o = "Bob Dole" if sum_fchae_o >= 270 & election == 1996
replace fnationalwinner_o = "Al Gore" if sum_fince_o >= 270 & election == 2000
replace fnationalwinner_o = "George W. Bush" if sum_fchae_o >= 270 & election == 2000
replace fnationalwinner_o = "George W. Bush" if sum_fince_o >= 270 & election == 2004
replace fnationalwinner_o = "John Kerry" if sum_fchae_o >= 270 & election == 2004
replace fnationalwinner_o = "John McCain" if sum_fince_o >= 270 & election == 2008
replace fnationalwinner_o = "Barack Obama" if sum_fchae_o >= 270 & election == 2008
replace fnationalwinner_o = "Barack Obama" if sum_fince_o >= 270 & election == 2012
replace fnationalwinner_o = "Mitt Romney" if sum_fchae_o >= 270 & election == 2012
replace fnationalwinner_o = "Hillary Clinton" if sum_fince_o >= 270 & election == 2016
replace fnationalwinner_o = "Donald Trump" if sum_fchae_o >= 270 & election == 2016

* Correct state out-of-sample forecast?

gen statecorrect_o = "Yes" if fstatewinner_o == statewinner & election!=2020
replace statecorrect_o = "No" if fstatewinner_o!=statewinner & election!=2020

* Correct natioanl out-of-sample forecast?

gen nationalcorrect_o = "Yes" if fnationalwinner_o == nationalwinner & election!=2020
replace nationalcorrect_o = "No" if fnationalwinner_o!=nationalwinner & election!=2020

* Summary

list election abbr twoincv ftwoincv_o ftwochav_o err_o abserr_o mabserr_o ince fince_o chae fchae_o statecorrect_o if election!=2020


**********************************************************************
** VOTE SHARE FORECASTS BY STATE FOR THE 2020 PRESIDENTIAL ELECTION **
**********************************************************************

reg twoincv unemp legcont jpa2 jpa0 ppi5216 ppi8016 indv chavp rhsc dhsc dcds dcrs if election!=2020

* Predicted vote share by state for Donald Trump, 2020 (see Figure 1, panel (a) in the article; see also Table L1 in the appendix)

gen trump_v_2020 = _b[_cons] + _b[unemp]*unemp + _b[legcont]*legcont + _b[jpa2]*jpa2 + _b[jpa0]*jpa0 + _b[ppi5216]*ppi5216 + ///
_b[ppi8016]*ppi8016 + _b[indv]*indv + _b[chavp]*chavp + _b[rhsc]*rhsc + _b[dhsc]*dhsc + _b[dcds]*dcds + ///
_b[dcrs]*dcrs if election == 2020

* Predicted vote share by state for Joe Biden, 2020 (see Figure 1, panel (b) in the article; see also Table L1 in the appendix)

gen biden_v_2020 = 100 - trump_v_2020 if election == 2020

* Side-by-side comparison

list biden_v_2020 trump_v_2020 if election == 2020


************************************************************************
** ELECTORAL COLLEGE VOTE FORECAST FOR THE 2020 PRESIDENTIAL ELECTION **
************************************************************************

* Predicted Electoral College votes by state for Donald Trump, 2020 (see Figure 1, panel (c) in the article; see also Table L1 in the appendix)

gen trump_e_2020 = elecvote if trump_v_2020 > biden_v_2020 & election == 2020

* Predicted Electoral College votes by state for Joe Biden, 2020 (see Figure 1, panel (c) in the article; see also Table L1 in the appendix)

gen biden_e_2020 = elecvote if trump_v_2020 < biden_v_2020 & election == 2020

* Total predicted Electoral College votes by state for Donald Trump, 2020

egen sum_trump_e_2020 = sum(trump_e_2020) if election == 2020

* Total predicted Electoral College votes by state for Joe Biden, 2020

egen sum_biden_e_2020 = sum(biden_e_2020) if election == 2020

* Predicted winner in each state, 2020

gen fstatewinner_2020 = "Donald Trump" if trump_v_2020 > biden_v_2020
replace fstatewinner_2020 = "Joe Biden" if trump_v_2020 < biden_v_2020

* Side-by-side comparison

list biden_e_2020 trump_e_2020 sum_biden_e_2020 sum_trump_e_2020 if election == 2020


***********************************************************************************
** PROBABILITY OF CROSSING THE 50% MARK IN A STATE USING OUT-OF-SAMPLE FORECASTS **
***********************************************************************************

* Probability for incumbent candidate, 1980-2016

gen inc_pr_o = .

estimates restore reg1980_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 1980

estimates restore reg1984_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 1984

estimates restore reg1988_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 1988

estimates restore reg1992_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 1992

estimates restore reg1996_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 1996

estimates restore reg2000_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 2000

estimates restore reg2004_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 2004

estimates restore reg2008_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 2008

estimates restore reg2012_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 2012

estimates restore reg2016_o
replace inc_pr_o = (1 - normal((50 - ftwoincv_o)/e(rmse)))*100 if election == 2016

* Probability for challenger candidate, 1980-2016

gen cha_pr_o = .

estimates restore reg1980_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 1980

estimates restore reg1984_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 1984

estimates restore reg1988_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 1988

estimates restore reg1992_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 1992

estimates restore reg1996_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 1996

estimates restore reg2000_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 2000

estimates restore reg2004_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 2004

estimates restore reg2008_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 2008

estimates restore reg2012_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 2012

estimates restore reg2016_o
replace cha_pr_o = (1 - normal((50 - ftwochav_o)/e(rmse)))*100 if election == 2016

* Probability for Donald Trump, 2020

estimates restore reg2020
gen trump_pr_2020 = (1 - normal((50 - trump_v_2020)/e(rmse)))*100 if election == 2020

* Probability for Joe Biden, 2020

estimates restore reg2020
gen biden_pr_2020 = (1 - normal((50 - biden_v_2020)/e(rmse)))*100 if election == 2020


**************************************************
** POPULAR VOTE FORECASTS AT THE NATIONAL LEVEL **
**************************************************

* Weighted two-party vote share forecast by state for Hillary Clinton, 2016

gen clinton_wpv_2016 = (registered/157596)*ftwoincv_o if election == 2016

* National two-party vote share forecast for Hillary Clinton, 2016

egen clinton_npv_2016 = sum(clinton_wpv_2016) if election == 2016

* Weighted two-party vote share forecast by state for Donald Trump, 2016

gen trump_wpv_2016 = (registered/157596)*ftwochav_o if election == 2016

* National two-party vote share forecast for Donald Trump, 2016

egen trump_npv_2016 = sum(trump_wpv_2016) if election == 2016

* Weighted two-party vote share forecast by state for Donald Trump, 2020

gen trump_wpv_2020 = (registered/157596)*trump_v_2020 if election == 2020

* National two-party vote share forecast for Donald Trump, 2020

egen trump_npv_2020 = sum(trump_wpv_2020) if election == 2020

* Weighted two-party vote share forecast by state for Joe Biden, 2020

gen biden_wpv_2020 = (registered/157596)*biden_v_2020 if election == 2020

* National two-party vote share forecast for Joe Biden, 2020

egen biden_npv_2020 = sum(biden_wpv_2020) if election == 2020

preserve


****************************
** SAVE NEW FILE FOR MAPS **
****************************

save forecasts_1980_2020, replace 

use "forecasts_1980_2020.dta", clear

keep election state elecvote statewinner fstatewinner_o ftwoincv_o ftwochav_o statecorrect_o trump_v_2020 biden_v_2020 fstatewinner_2020

replace statecorrect_o = "Correct" if statecorrect_o == "Yes"
replace statecorrect_o = "Incorrect" if statecorrect_o == "No"

save forecasts_1980_2020, replace // This file is needed to reproduce the cartograms and choropleth maps presented in the article and the appendix (these figures were created using the R file "figures_2spe_model.R")

restore


********************
** CLOSE LOG FILE **
********************

log close